<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use think\Db;
use think\Hook;

/**
 * 经纪机器人接口
 */
class AgentRobot extends Api
{
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\api\model\DbCommon');
    }

    //登录
    public function login(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "password",
            "robotType",
            "IP",
            "version",
        ];
        
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "res",
        ];
        //查询
        $procedure_name = 'robot_login';
        $res = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
       
        if($res['res']){
            $account = $this->request->post('robotUser');
            //echo $account;die;
            $password = $this->request->post('password');
            if (!$account || !$password) {
                $this->error(__('Invalid parameters'),[]);
            }
            $ret = $this->auth->login2($account, $password);
            if ($ret) {
                $userinfo = $this->auth->getUserinfo();

                $userinfo['robotUser'] = $params['robotUser'];
                unset($userinfo['user_id']);
                $data = ['userinfo' => $userinfo];

                $this->success(__('Logged in successful'), $data);
            } else {
                $this->error($this->auth->getError(),[]);
            }
        }else{
            $this->error(__('登录失败'),[]);
        }

        $this->success('返回成功', $res);
    }


    //1读取机器人参数
    public function param_get(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "fansInterval",
            "fansMaxTask",
            "videoInterval",
            "videoMaxTask",
            "videoMaxLikes",
            "videoMaxSpeak",
            "checkInterval",
            "checkMaxTask",
        ];
        //查询
        $procedure_name = 'agentrobot_param_get';
        
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        $this->success('返回成功', $list);
    }

    //2读取需要关注的用户列表
    public function getsayhellolist(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "agentrobotUser",
            "lastdateNum",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $in_out_column = [
            "userCount"
        ];
        $out_column = [
        ];

        //查询
        $procedure_name = 'agentrobot_get_sayhello_list';
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column,$in_out_column);
        $this->success('查询用户成功', $list);
    }

    //3添加/更新打招呼数据(主播打招呼登记)
    public function logsayhelloresult(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "livestreamerUser",
            "isFollow",
            "videoLikes",
            "videoComments",
        ];
        $out_column = [
            "res",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'agentrobot_sayhello_log';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('登记结果成功', $rs);
        }else{
            $this->error('登记结果失败',$rs);
        }
    }

    //4取消关注登记
    public function logcancelfollow(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "livestreamerUser",
        ];
        $out_column = [
            "res",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'agentrobot_cancefollow_log';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('设置成功', $rs);
        }else{
            $this->error(__('找不到记录'),$rs);
        }
    }

    //5-已回关主播登记
    public function logrefollowlist(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "agentrobotUser",
            "livestreamerUser",
        ];
        $out_column = [
            "res",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'agentrobot_refollow_log';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('设置成功', $rs);
        }else{
            $this->error(__('找不到记录'),$rs);
        }
    }

}
